<template>
  <div>
    
    <!-- 分割线 -->
    <el-divider></el-divider>
    <!-- 搜索表单 -->
    <el-form
      @submit.native.prevent
      class="demo-form-inline"
      :inline="true"
      :model="form"
    >
      <el-form-item label="电影名称">
        <el-input
          @keyup.native.enter="onSubmit"
          placeholder="请输入电影名称"
          v-model="form.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 分割线 -->
    <el-divider content-position="left">电影列表</el-divider>
    <!-- 表格 -->
    <el-table :data="moviesData.result">
      <el-table-column label="封面图片">
        <template slot-scope="scope">
          <img :src="scope.row.cover" width="50px" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title"> </el-table-column>
      <el-table-column label="主演" prop="star_actor"> </el-table-column>
      <el-table-column label="上映时间" prop="showingon"> </el-table-column>
      <el-table-column label="时长">
        <template slot-scope="scope"> {{ scope.row.duration }}分钟 </template>
      </el-table-column>
      <el-table-column label="所属类别">
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="info"
            icon="el-icon-user-solid"
            circle
          ></el-button>
          <el-button
            size="small"
            type="success"
            icon="el-icon-picture-outline"
            circle
          ></el-button>
          <el-button
          @click="$router.push(`/home/movie-update/${scope.row.id}`)"
            size="small"
            type="primary"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            @click="delItem(scope.row.id,scope.row.title)"
            size="small"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="->,prev, pager, next,total,jumper"
      :current-page="moviesData.page"
      :page-size="moviesData.pagesize"
      :total="moviesData.total"
      @current-change="pageChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      keyword: "",
      form: {
        name: "",
      },
      moviesData: {}, // 保存当前页面中显示的电影列表
    };
  },
  methods: {
    // 删除电影列表，发烧请求
    delItem(id,title) {
      
      this.$confirm(`该操作将永久删除电影:${title},是否继续`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.$httpApi.movieApi.delete({ id }).then((res) => {
        // 刷新当前列表，整理参数发送请求
        // 有keyword带着一起模糊查询
        // 有数据带着数据查询
        // 没有keyword一起查询
        this.$message({
            type: 'success',
            message: '删除成功!'
          });
        if (this.keyword) {
          let params = { name: this.keyword, page:this.moviesData.page, pagesize:5};
          this.$httpApi.movieApi.queryByNameLike(params).then((res) => {
            this.moviesData = res.data.data;
          });
        } else {
          // pagination组件抛出自定义事件，并且传递参数
          //    查询所有电影
          let params = { page:this.moviesData.page, pagesize: 5 };
          this.$httpApi.movieApi.queryAll(params).then((res) => {
            this.moviesData = res.data.data;
          });
        }
      });
          
        }).catch(() => {
          this.$message({
            
            type: 'info',
            message: '已取消删除'
          });          
        });
      
    },
    // 分页查询
    pageChange(page) {
      // 有数据带着数据查询
      if (this.keyword) {
        let params = { name: this.keyword, page, pagesize: 5 };
        this.$httpApi.movieApi.queryByNameLike(params).then((res) => {
          this.moviesData = res.data.data;
        });
      } else {
        // pagination组件抛出自定义事件，并且传递参数
        let params = { page, pagesize: 5 };
        this.$httpApi.movieApi.queryAll(params).then((res) => {
          console.log(res);
          this.moviesData = res.data.data;
        });
      }
    },
    onSubmit() {
      this.keyword = this.form.name;
      if (this.form.name.trim()) {
        let params = { name: this.form.name, page: 1, pagesize: 5 };
        this.$httpApi.movieApi.queryByNameLike(params).then((res) => {
          this.moviesData = res.data.data;
        });
      } else {
        // pagination组件抛出自定义事件，并且传递参数
        let params = { page, pagesize: 5 };
        this.$httpApi.movieApi.queryAll(params).then((res) => {
          console.log(res);
          this.moviesData = res.data.data;
        });
      }
    },
  },
  mounted() {
    let params = { page: 1, pagesize: 5 };
    this.$httpApi.movieApi.queryAll(params).then((res) => {
      console.log(res);
      this.moviesData = res.data.data;
    });
  },
};
</script>

<style lang="scss" scoped>
</style>